package cn.tedu.charging.order.dao.repository.impl;

import cn.tedu.charging.order.common.OrderStatusConstants;
import cn.tedu.charging.order.dao.mapper.OrderFailMapper;
import cn.tedu.charging.order.dao.mapper.OrderSuccessMapper;
import cn.tedu.charging.order.dao.repository.OrderRepository;
import cn.tedu.charging.order.pojo.po.ChargingOrderFailPO;
import cn.tedu.charging.order.pojo.po.ChargingOrderSuccessPO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
//java 中的静态导入
//import static cn.tedu.charging.order.common.OrderStatusConstants.ORDER_STATUS_EXCEPTION_END;

@Repository
public class OrderRepositoryImpl implements OrderRepository {

    @Autowired
    private OrderSuccessMapper orderSuccessMapper;

    @Autowired
    private OrderFailMapper orderFailMapper;

    @Override
    public ChargingOrderSuccessPO getSuccessOrderByOrderNo(String orderNo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("bill_id", orderNo);
        return orderSuccessMapper.selectOne(queryWrapper);
    }

    @Override
    public ChargingOrderFailPO getFailOrderByOrderNo(String orderNo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("bill_id", orderNo);
        return orderFailMapper.selectOne(queryWrapper);
    }

    @Override
    public int insertFailOrder(ChargingOrderFailPO chargingOrderFailPO) {
        return orderFailMapper.insert(chargingOrderFailPO);
    }

    @Override
    public Integer updateOrderStatusFromChargingToExceptionEnd(String orderNo) {
        //1> 通过订单号查询成功订单 并判断 并更新

        // select status from order_success where bill_id = orderNo

        // if (status = 充电中) {
        //    update xxx set status = 异常结束 where bill_id = orderNo
        // }



        //2>  update xxx set status = 异常结束 where bill_id = orderNo and status = 充电中

        //for i in list
        //{
        //   select * from xx where id = i;
        //}
        // ids = ""
        //for i in list {
        //    ids = ids + i;
        //}
        //select * from xx where id in ids;

        //update xxx
        //  set status = 异常结束
        // where
        //  bill_id = orderNo and status = 充电中

        //要更新的目标值
        ChargingOrderSuccessPO chargingOrderSuccessPO = new ChargingOrderSuccessPO();
        chargingOrderSuccessPO.setBillStatus(OrderStatusConstants.ORDER_STATUS_EXCEPTION_END);

        //UpdateWrapper 是设置 执行sql的条件
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("bill_id",orderNo);
        updateWrapper.eq("bill_status", OrderStatusConstants.ORDER_STATUS_CHARGING);

        return orderSuccessMapper.update(chargingOrderSuccessPO,updateWrapper);
    }
}
